package com.jyw.business.ast.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.jyw.business.ast.constant.AstRelationConstant;
import com.jyw.business.ast.domain.AstDealRelation;
import com.jyw.business.ast.domain.AstDispose;
import com.jyw.business.ast.domain.AstInfo;
import com.jyw.business.ast.domain.dto.web.AstTransferDto;
import com.jyw.business.ast.domain.dto.web.AstTransferPageDto;
import com.jyw.business.ast.domain.dto.web.AstRejectedDto;
import com.jyw.business.ast.domain.vo.web.AstTransferAuditPageVo;
import com.jyw.business.ast.domain.vo.web.AstTransferDetailVo;
import com.jyw.business.ast.domain.vo.web.AstTransferPageVo;
import com.jyw.business.ast.domain.vo.web.TransferAstVo;
import com.jyw.business.ast.service.IAstDealRelationService;
import com.jyw.business.ast.service.IAstInfoService;
import com.jyw.common.exception.ServiceException;
import com.jyw.common.utils.DateUtils;
import com.jyw.common.utils.SecurityUtils;
import com.jyw.system.utils.ConversionUtil;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import com.jyw.business.ast.mapper.AstTransferMapper;
import com.jyw.business.ast.domain.AstTransfer;
import com.jyw.business.ast.service.IAstTransferService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;

/**
 * 资产-转移信息Service业务层处理
 * 
 * @author system
 * @date 2023-10-19
 */
@Service
public class AstTransferServiceImpl extends ServiceImpl<AstTransferMapper,AstTransfer> implements IAstTransferService
{
    @Resource
    private AstTransferMapper astTransferMapper;

    @Resource
    private IAstDealRelationService relationService;

    @Resource
    private IAstInfoService astInfoService;

    /**
     * 查询资产-转移信息
     * 
     * @param id 资产-转移信息主键
     * @return 资产-转移信息
     */
    @Override
    public AstTransfer selectAstTransferById(Long id)
    {
        return astTransferMapper.selectAstTransferById(id);
    }

    /**
     * 查询资产-转移信息列表
     * 
     * @param astTransfer 资产-转移信息
     * @return 资产-转移信息
     */
    @Override
    public List<AstTransfer> selectAstTransferList(AstTransfer astTransfer)
    {
        return astTransferMapper.selectAstTransferList(astTransfer);
    }

    /**
     * 新增资产-转移信息
     * 
     * @param astTransfer 资产-转移信息
     * @return 结果
     */
    @Override
    public int insertAstTransfer(AstTransfer astTransfer)
    {
        astTransfer.setCreateTime(DateUtils.getNowDate());
        return astTransferMapper.insertAstTransfer(astTransfer);
    }

    /**
     * 修改资产-转移信息
     * 
     * @param astTransfer 资产-转移信息
     * @return 结果
     */
    @Override
    public int updateAstTransfer(AstTransfer astTransfer)
    {
        return astTransferMapper.updateAstTransfer(astTransfer);
    }

    /**
     * 批量删除资产-转移信息
     * 
     * @param ids 需要删除的资产-转移信息主键
     * @return 结果
     */
    @Override
    public int deleteAstTransferByIds(Long[] ids)
    {
        return astTransferMapper.deleteAstTransferByIds(ids);
    }

    /**
     * 删除资产-转移信息信息
     * 
     * @param id 资产-转移信息主键
     * @return 结果
     */
    @Override
    public int deleteAstTransferById(Long id)
    {
        return astTransferMapper.deleteAstTransferById(id);
    }

    @Override
    public List<AstTransferPageVo> transferPage(AstTransferPageDto dto) {
        //设置转入科室
        dto.setInDept(SecurityUtils.getDeptId());
        //设置结束时间
        dto.setCreateEndTime(DateUtils.toDateLastTime(dto.getCreateEndTime()));
        return ConversionUtil.conversionList(astTransferMapper.transferPage(dto),AstTransferPageVo.class);
    }

    @Override
    public AstTransferDetailVo transferDetail(Long id) {
        AstTransfer transfer = getById(id);
        AstTransferDetailVo detail = new AstTransferDetailVo(transfer);
        List<TransferAstVo> list = relationService.transferRelation(id);
        detail.setList(list);
        return ConversionUtil.conversion(detail,AstTransferDetailVo.class);
    }

    @Override
    public void transfer(AstTransferDto dto) {
        AstTransfer transfer = dto.buildTransfer();
        //存入转移信息
        save(transfer);
        //存入资产信息
        List<AstDealRelation> list = dto.buildRelation(transfer);
        relationService.saveBatch(list);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void revoke(Long id) {
        AstTransfer transfer = getById(id);
        if(ObjectUtils.isEmpty(transfer)){
            throw new ServiceException("转移记录不存在");
        }
        if(!transfer.getAuditState().equals("0")){
            throw new ServiceException("已审核,无法撤销");
        }
        //删除关系
        relationService
                .lambdaUpdate()
                .eq(AstDealRelation::getType, AstRelationConstant.TRANSFER)
                .eq(AstDealRelation::getBusinessId,id)
                .remove();
        //删除转移信息表
        removeById(id);
    }

    @Override
    public List<AstTransferAuditPageVo> transferAuditPage(AstTransferPageDto dto) {
        //设置转入科室
        dto.setOutDept(SecurityUtils.getDeptId());
        //设置结束时间
        dto.setCreateEndTime(DateUtils.toDateLastTime(dto.getCreateEndTime()));
        return ConversionUtil.conversionList(astTransferMapper.transferAuditPage(dto),AstTransferAuditPageVo.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void pass(List<Long> list) {
        list = lambdaQuery()
                .in(AstTransfer::getId,list)
                .eq(AstTransfer::getAuditState,0)
                .list().stream().map(AstTransfer::getId).collect(Collectors.toList());
        if(!CollectionUtils.isEmpty(list)){
            //获取资产信息
            List<AstDealRelation> relationList = relationService
                    .lambdaQuery()
                    .eq(AstDealRelation::getType, AstRelationConstant.TRANSFER)
                    .in(AstDealRelation::getBusinessId, list)
                    .list();
            //封装资产使用科室修改信息
            List<AstInfo> astUpdateList = relationList.stream().map(data -> {
                AstInfo info = new AstInfo();
                info.setId(data.getAstId());
                info.setUseDept(data.getInDept());
                return info;
            }).collect(Collectors.toList());
            //修改资产使用科室
            astInfoService.updateBatchById(astUpdateList);
            //批量修改转移信息
            lambdaUpdate()
                    .in(AstTransfer::getId,list)
                    .set(AstTransfer::getAuditUser,SecurityUtils.getUserId())
                    .set(AstTransfer::getAuditState,"2")
                    .update();
        }

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void auditRejected(AstRejectedDto dto) {
        lambdaUpdate()
                .eq(AstTransfer::getId,dto.getId())
                //设置审核状态为驳回
                .set(AstTransfer::getAuditState,"1")
                .set(AstTransfer::getAuditUser,SecurityUtils.getUserId())
                //设置备注
                .set(AstTransfer::getAuditRemark,dto.getRemark())
                .update();
    }

}
